package com.deng.maintenance.vote.view
{
	import com.deng.maintenance.vote.vo.VoteValueVo;
	
	import flash.events.KeyboardEvent;
	
	import mx.controls.Alert;
	import mx.controls.DataGrid;
	import mx.controls.TextInput;
	import mx.events.FlexEvent;
	import mx.resources.ResourceManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	import mx.utils.StringUtil;

	public class PointOperateIT extends TextInput
	{
		private var pvo:VoteValueVo;
		public function PointOperateIT()
		{	
			this.restrict = "0-9\.";
			this.width = 40;
			this.addEventListener(KeyboardEvent.KEY_UP,updateData);
			this.addEventListener(FlexEvent.CREATION_COMPLETE,showHandler);
			this.addEventListener(FlexEvent.SHOW,showHandler);
			super();
		}
		private function updateRst(evt:ResultEvent):void{
			Alert.show(ResourceManager.getInstance().getString("lang", "update_success"));
			this.data = pvo;
		}
		private function updateData(event:KeyboardEvent):void{
			if(event.keyCode == 13){
				if(isNaN(new Number(this.text))){
					Alert.show(ResourceManager.getInstance().getString("lang", "input_item_error"));
					return;
				}
				if(pvo.score == StringUtil.trim(this.text)){
					return;
				}
				
				var ro:RemoteObject = new RemoteObject();
				ro.destination = "VoteValueImpl";			
				ro.addEventListener(FaultEvent.FAULT,flt);
				ro.addEventListener(ResultEvent.RESULT,updateRst);
				if( (pvo.score != null && pvo.score != "") 
					&& StringUtil.trim(this.text) == ""){//原来有，后来删除
					pvo.score = null;
					ro.deleteDataOperate(pvo);
				}else if( (pvo.score != null && StringUtil.trim(pvo.score) != "") 
					&& StringUtil.trim(this.text) != ""
					&& pvo.score != StringUtil.trim(this.text)){//修改
					pvo.score = StringUtil.trim(this.text);
					ro.updateValueOperate(pvo);
				}else{//增加
					try{
						pvo.score = StringUtil.trim(this.text);
						ro.insertDataOperate(pvo);
					}catch(err:Error){
						Alert.show(err.message+" ... "+err.getStackTrace());
					}
				}
				(this.parent.parent as DataGrid).setFocus();
				
			}
			
			
			
		}
		private function flt(evt:FaultEvent):void{
			Alert.show(" : "+evt.fault.getStackTrace());
		}
		private function showHandler(event:FlexEvent):void{
			pvo = this.data as VoteValueVo;
			if(pvo.valid == 1){
				this.enabled = false;
			}else{
				this.enabled = true;
			}
			if(pvo.score == null){
				this.text = "";
			}else{
				this.text = pvo.score;
			}
		}
	}
}